﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.SignalR;
using RTW.Core.Extensions;
using RTW.Models;
using RTW.Servers;
using RTW.Web.Comm;

namespace RTW.Web.Areas.System.Controllers
{
    [Area("System")]
    public class MenuController : CommController
    {
        TMenuServer _tmenuServer;
        public MenuController()
        {
            _tmenuServer = new TMenuServer();
        }

        // GET: Menu
        public IActionResult MenuManager()
        {
            ViewBag.reurl = HttpContext.Request.GetAbsoluteUri();
            return  View();
        }

        // GET: Menu/Details/5
        public async Task<IActionResult> DelMenu(string id)
        {
            var success = await  _tmenuServer.LogicalDelete(id);
            return Json(MsgResult(success));
        }

        // GET: Menu/Create
        public async Task<IActionResult> MenuEdit(string id)
        {
            IList<TMenu> list = _tmenuServer.GetMenuTreeCte();
            List<SelectListItem> listitem = new List<SelectListItem>();
            listitem.Add(new SelectListItem { Text = "主菜单", Value = "" });
            foreach (var dep in list)
            {
                listitem.Add(new SelectListItem
                {
                    Text = dep.MenuName,
                    Value = dep.ID
                });
            }
            ViewBag.database = listitem;

            TMenu _menu = new TMenu();
            //编辑或新增
            if (string.IsNullOrEmpty(id))
            {

            }
            else
            {
                //修改时回绑字段
                _menu = await _tmenuServer.GetByOneAsync(m => m.ID == id);
            }

            return View(_menu);
        }

        // GET: Menu/Create
        public async Task<IActionResult> GetMenuForJson()
        {
            var success = await _tmenuServer.GetMenuForJson(UserAccount == "admin"?null:UserDMID);

            var str = Json(success);
            return  str;
        }



        // POST: Menu/Edit/5
        [HttpPost]
        public async Task<IActionResult> MenuEdit(string id, TMenu menu)
        {
            IList<TMenu> list = _tmenuServer.GetMenuTreeCte();
            List<SelectListItem> listitem = new List<SelectListItem>();
            listitem.Add(new SelectListItem { Text = "主菜单", Value = "" });
            foreach (var dep in list)
            {
                listitem.Add(new SelectListItem
                {
                    Text = dep.MenuName,
                    Value = dep.ID
                });
            }
            ViewBag.database = listitem;

            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    //新增
                    menu.ID = Guid.NewGuid().ToString("N");
                    menu.Parent_ID = menu.Bak2;//更新父节点ID
                    menu.DMID = UserDMID;
                    menu.Bak2= menu.Bak2.ToString();
                    await  _tmenuServer.InsertAsync(menu);


                    AddLog("添加了菜单（" + menu.MenuName + "）", "菜单管理", Request.GetAbsoluteUri());
                }
                else
                {
                    //修改
                    menu.Parent_ID = menu.Bak2;//更新父节点ID
                    await _tmenuServer.UpdateAsync(menu);

                    AddLog("修改了菜单（" + menu.MenuName + "）", "菜单管理", Request.GetAbsoluteUri());
                }

            }
            catch(Exception e)
            {
                AddErrLog(e.Message,UserDMID);
                return Json(MsgResult(false));
            }
            //_context.Clients.All.ShowTips("菜单信息保存成功!", 2);

            // return View();

            return Json(MsgResult(true));
        }
    }
}